#include "TextManager.h"
using namespace std;

TextManager* TextManager::mInstance = 0;

TextManager::TextManager()
{
	if(TTF_Init() == -1)
	{
		cout << "Error creating TextManager Instance" << endl;
		Kill();
	}
	else
	{
		LoadFont("arial.ttf", 20);
		SetFontColor(new SDL_Color());
		SetRenderMethod(SOLID);
	}
}

TextManager::~TextManager()
{
	delete mFont;
	mFont = 0;
	delete mColor;
	mColor = 0;
	if(mFont != 0)
		TTF_CloseFont(mFont);
	TTF_Quit();
}

void TextManager::LoadFont(string fontName, int size)
{
	TTF_Font* thisFont = SDLResources::GetInstance()->GetFont(fontName, size);
	if(thisFont != 0)
		mFont = thisFont;
	else
	{
		cout << "Cannot load font at '" << fontName << "'." << endl;
		mFont = 0;
	}
}

void TextManager::SetFontColor(SDL_Color* color)
{
	mColor = color;
}

SDL_Surface* TextManager::RenderText(string text)
{
	if(mFont != 0)
	{
		switch(mRenderMethod)
		{
			case SOLID: return TTF_RenderText_Solid(mFont, text.c_str(), *mColor); break;
			case ANTIALIAS: return TTF_RenderText_Blended(mFont, text.c_str(), *mColor); break;
			default: return TTF_RenderText_Solid(mFont, "Error", *mColor);
		}
	}
	else
	{
		cout << "Invalid font, cannot render text." << endl;
		return 0;
	}
}